package cn.wing.gui;

import cn.wing.Inventory.PocketInventory;
import cn.wing.event.TransactionInfoMapSync;
import cn.wing.utils.TransactionInfo;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.UUID;

import static cn.wing.PlayTradingMain.TRADING_MENU_TYPE;
import static cn.wing.PlayTradingMain.TRANSACTION_INFO_MAP_CHANNEL;

public class TradingMenu extends AbstractContainerMenu {

    private static final Logger log = LoggerFactory.getLogger(TradingMenu.class);

    private final ItemStackHandler inv;

    public TradingMenu(int p_38852_, Inventory p_39644_, Container constant) {
        super(TRADING_MENU_TYPE.get(), p_38852_);
//        this.constant = constant;
        this.inv = p_39644_.player.getCapability(PocketInventory.CAP, null).orElseThrow(RuntimeException::new);
        this.addSlot(new SlotItemHandler(inv, 0, 25, 20));
        for(int l = 0; l < 3; ++l) {
            for(int j1 = 0; j1 < 9; ++j1) {
                this.addSlot(new Slot(p_39644_, j1 + l * 9 + 9, 8 + j1 * 18, l * 18 + 51));
            }
        }

        for(int i1 = 0; i1 < 9; ++i1) {
            this.addSlot(new Slot(p_39644_, i1, 8 + i1 * 18, 109));
        }
    }

    public TradingMenu(int p_38852_, Inventory p_39644_) {
        this(p_38852_, p_39644_, new SimpleContainer(1));

    }

    @Override
    public @NotNull ItemStack quickMoveStack(Player p_38941_, int p_38942_) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.slots.get(p_38942_);
        if (slot.hasItem()) {
            ItemStack itemstack1 = slot.getItem();
            itemstack = itemstack1.copy();
            if (p_38942_ < inv.getSlots()) {
                if (!this.moveItemStackTo(itemstack1, inv.getSlots(), this.slots.size(), false)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.moveItemStackTo(itemstack1, 0, inv.getSlots(), false)) {
                return ItemStack.EMPTY;
            }

            if (itemstack1.isEmpty()) {
                slot.setByPlayer(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }
        }

        return itemstack;
    }

    @Override
    public boolean stillValid(Player p_38874_) {
        return true;
    }

    public void sell(int index, UUID player) {
        if (this.getSlot(0).hasItem()) {
            ItemStack itemStack = new ItemStack(Items.EMERALD, index);
            ArrayList<ItemStack> itemStacks = new ArrayList<>();
            itemStacks.add(itemStack);
            TransactionInfo transactionInfo = new TransactionInfo(player, this.getSlot(0).getItem().copy(), itemStacks, "xxx");
            this.getSlot(0).getItem().setCount(0);
            this.getSlot(0).setByPlayer(ItemStack.EMPTY);
            this.getSlot(0).setChanged();
            TRANSACTION_INFO_MAP_CHANNEL.sendToServer(new TransactionInfoMapSync(transactionInfo.getUuid(), transactionInfo));
        }
    }

    public void addItem(ItemStack itemStack) {
        this.moveItemStackTo(itemStack, inv.getSlots(), this.slots.size(), false);
    }

    public void clear(int index, int count) {
        ItemStack item = this.getSlot(index).getItem();
        if (item.getCount() <= count) {
            item.setCount(0);
            this.getSlot(index).setByPlayer(ItemStack.EMPTY);
        } else {
            item.setCount(item.getCount() - count);
        }
        this.getSlot(index).setChanged();
    }



}
